PROJECT_PATH = .

############ 模块信息    #############
MODULE_NAME := RUN
MODULE_TARGET := run

SUB_ROOT = $(PROJECT_PATH)/tools

SUB_MODULE := Math Math2


# 编译工具
AR = ar
CC = gcc
CXX = g++

# 依赖库, 编译参数
LIBS_DIR += -L./
CFLAGS := -g -w

LIBS += -lpthread

## 编译产物
OUT_PATH := $(PROJECT_PATH)/out
TARGET := $(OUT_PATH)/$(MODULE_TARGET)

ifndef $(wildcard $(OUT_PATH))
$(shell mkdir -p $(OUT_PATH) )
endif

### 头文件路径
INC_DIR := -I$(PROJECT_PATH)/inc


#### 源码路径

SRC_CPP := $(PROJECT_PATH)/src
SRC_C := $(PROJECT_PATH)/src
### OBJ 路径
OBJ_DIR := $(OUT_PATH)

### 目标文件
OBJ_CPP := $(patsubst $(SRC_CPP)/%.cpp,$(OBJ_DIR)/%.o, $(wildcard $(SRC_CPP)/*.cpp))
OBJ_C := $(patsubst $(SRC_C)/%.c,$(OBJ_DIR)/%.o, $(wildcard $(SRC_C)/*.c))
$(info $(OBJ_CPP))




###### 子项目依赖    ######
### 存放子模块的根目录

SUB_PATH := $(foreach x, $(SUB_MODULE), $(SUB_ROOT)/$(x))

SUB_OBJ := $(foreach x, $(SUB_MODULE), $(SUB_ROOT)/$(x)/out/$(x).a)

INC_DIR += $(foreach x, $(SUB_MODULE), -I$(SUB_ROOT)/$(x)/inc)
INC_DIR += $(foreach x, $(SUB_MODULE), -I$(SUB_ROOT)/$(x))


$(info $(INC_DIR))

LIB_STATIC := $(SUB_OBJ)




CFLAGS += $(INC_DIR) $(LIBS) $(LIBS_DIR)

################
## 编译目标, 依赖
.PHONY:all
all: $(TARGET)

$(TARGET): $(OBJ_CPP) $(OBJ_C) $(SUB_OBJ) 
	$(CXX) -o $@ $^ $(CFLAGS)

## .cpp 目标文件
$(OBJ_DIR)/%.o: $(SRC_CPP)/%.cpp
	$(CXX) -o $@ -c $< $(CFLAGS)

$(OBJ_DIR)/%.o: $(SRC_CPP)/%.c
	$(CXX) -o $@ -c $< $(CFLAGS)

# 子模块
$(SUB_OBJ) : $(SUB_PATH)
	for sub_dir in $(SUB_PATH); \
	do $(MAKE) -C $$sub_dir || exit 1;\
	done
	

#### 清理
.PHONY:clean clean_all

clean_all: clean 
	for sub_dir in $(SUB_PATH); \
	do $(MAKE) -C $$sub_dir clean || exit 1;\
	done

clean:
	@-rm $(OUT_PATH)/*.o
	@-rm $(TARGET)


